import pandas as pd
import numpy as np
from pathlib import Path
from scipy.interpolate import RegularGridInterpolator

def get_performance(file_path: str, altitude_input: int, weight_input: float, config_approach_input: str) -> float:
    """
    使用线性插值法计算给定条件下的性能值。

    参数:
    file_path (str): Excel 文件的路径。
    altitude_input (float): 输入的高度值。
    weight_input (float): 输入的重量值。
    config_approach_input (str): 配置方式。

    返回:
    float: 计算得到的性能值。

    异常:
    FileNotFoundError: 如果文件路径无效。
    KeyError: 如果配置方式无效。
    """
    altitude = [-2000, -1000, 0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 11000, 12000, 13000, 14000, 14500]
    weight = [40, 50, 60, 70, 80]
    config_approach = ['0UP', '1UP', '5UP', '10UP', '15UP', '25UP', '15DOWN']
    columns = pd.MultiIndex.from_product([altitude, weight], names=['altitude', 'weight'])
    
    # 读取 Excel 文件
    df = pd.read_excel(file_path, header=None)
    df.columns = columns
    df.index = config_approach
    
    # 提取对应的行
    df = df.loc[config_approach_input]
    
    # 重新调整数据形状
    values = df.values.reshape(len(altitude), len(weight))
    
    # 创建插值函数
    interpolator = RegularGridInterpolator((altitude, weight), values)
    
    # 进行插值计算
    result = interpolator((altitude_input, weight_input))

    return float(result.round(2))

if __name__ == "__main__":
    root_path = Path('D:\\PYTHON\\QRH_data\\data')
    file_path = root_path / 'performance_data.xlsx'
    engine_rate = '22k'
    altitude_input = 5000
    weight_input = 60
    config_approach_input = '15UP'

    try:
        performance = get_performance(file_path, altitude_input, weight_input, config_approach_input)
        print(f"Performance: {performance}")
    except Exception as e:
        print(f"Error: {e}")